package com.btcode.orm.model.generater;

import com.btcode.db.executer.IDataExecuter;
import com.btcode.db.fieldtype.FieldType;
import com.btcode.db.result.Row;
import com.btcode.orm.conver.DefaultModelConver;
import com.btcode.orm.conver.IModelConver;
import java.util.List;

public class MySQLGenerater extends ModelGenerater {


    /**
     * @param executer
     */
    public MySQLGenerater(IDataExecuter executer) {
        super(executer);
    }

    /* (non-Javadoc)
     * @see com.bandit.orm.model.generater.ModelGenerater#getTableInfoModels()
     */
    @Override
    public List<TableInfoModel> getTableInfoModels(String tableName) {
        String sql =
            "SELECT column_name COLUMN_NAME,data_type DATA_TYPE FROM information_schema.columns"
                + " WHERE table_schema = '" + executer.getDataBaseConfig().getDbName() + "'"
                + " AND table_name = '" + tableName + "'";

        List<Row> cloumnss = executer.queryList(sql, null);
        IModelConver modelConver = new DefaultModelConver();
        List<TableInfoModel> tableInfoModels = modelConver.converToORMModels(cloumnss,
                                                                             TableInfoModel.class);

        return tableInfoModels;
    }

    /* (non-Javadoc)
     * @see com.bandit.orm.model.generater.ModelGenerater#getFieldJavaType(java.lang.String)
     */
    @Override
    public String getFieldJavaType(String type) {
        type = type.toUpperCase();

        if ("VARCHAR".equals(type)) {
            return "String";
        }
        else if ("DATETIME".equals(type)) {
            return "Date";
        }
        else if ("DOUBLE".equals(type)) {
            return "Double";
        }
        else {
            return "String";
        }
    }

    /* (non-Javadoc)
     * @see com.bandit.orm.model.generater.ModelGenerater#getFieldEmunType(java.lang.String)
     */
    @Override
    public FieldType getFieldEmunType(String type) {
        type = type.toUpperCase();

        if ("VARCHAR".equals(type) || "character".equals(type)) {
            return FieldType.String;
        }
        else if ("DATETIME".equals(type)) {
            return FieldType.DateTime;
        }
        else if ("DOUBLE".equals(type)) {
            return FieldType.Double;
        }
        else {
            return FieldType.String;
        }
    }

}
